/*
author      : 吴中勤 
date        : 2018/8/16
description : TODO
 */

package com.deepexi.adapter.converter.sap;

import com.deepexi.adapter.converter.MobyEventHandler;
import com.deepexi.adapter.converter.sap.bp.BPService;
import com.deepexi.adapter.converter.sap.bp.SapAdapterMember;
import com.deepexi.adapter.converter.sap.fund.FundService;
import com.deepexi.adapter.converter.sap.order.OrderService;
import com.deepexi.adapter.facade.moby.request.MobyEventBody;
import com.deepexi.adapter.facade.moby.response.BaseResponseBody;
import com.deepexi.adapter.sdk.sap.exception.SapBizException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MobyEventHandler4Sap implements MobyEventHandler {

    private static final Logger logger = LoggerFactory.getLogger(MobyEventHandler4Sap.class);

    @Autowired
    private SapConverterContext sapConverterContext;

    @Autowired
    private FundService fundService;

    @Autowired
    private BPService bpService;

    @Autowired
    private OrderService orderService;

    /** 将eventBody中的信息 关键索引数据 取出来, 可能其他地方用到 */
    private void setupConverterContext(MobyEventBody eventBody) {
        sapConverterContext.getLogContext()
                .setMobyOrderId(eventBody.getMobyTradeOrder().getId())
                .setShopCode(eventBody.getMobyTradeOrder().getShopCode())
                .setMemberId(eventBody.getMobyMember().getId());
    }

    @Override
    public BaseResponseBody onDepositPaid(MobyEventBody eventBody) throws Exception {
        if (null == eventBody) throw new SapBizException("无效事件, 消息内容=null");
        setupConverterContext(eventBody);
        fundService.onDepositPaid(eventBody);
        return BaseResponseBody.allOk();
    }

    @Override
    public BaseResponseBody onCoolingOffPeriodEnded(MobyEventBody eventBody) throws Exception {
        if (null == eventBody) throw new SapBizException("无效事件, 消息内容=null");

        setupConverterContext(eventBody);
        SapAdapterMember sapAdapterMember = bpService.createOrExtendBPIfNesessary(eventBody);
        sapConverterContext.setMemberBP(sapAdapterMember);
        orderService.createSapOrder(eventBody);

        return BaseResponseBody.allOk();
    }

    @Override
    public BaseResponseBody onDepositRefunded(MobyEventBody eventBody) throws Exception {
        if (null == eventBody) throw new SapBizException("无效事件, 消息内容=null");

        setupConverterContext(eventBody);
        fundService.onDepositRefunded(eventBody);

        return BaseResponseBody.allOk();
    }

    @Override
    public BaseResponseBody onVehicleLocked(MobyEventBody eventBody) throws Exception {
        if (null == eventBody) throw new SapBizException("无效事件, 消息内容=null");

        setupConverterContext(eventBody);
        orderService.onVehicleLocked(eventBody);

        return BaseResponseBody.allOk();
    }

    // 取消分车
    @Override
    public BaseResponseBody onVehicleUnlocked(MobyEventBody eventBody) throws Exception {
        if (null == eventBody) throw new SapBizException("无效事件, 消息内容=null");

        setupConverterContext(eventBody);
        orderService.onVehicleUnlocked(eventBody);
        return BaseResponseBody.allOk();
    }

    @Override
    public BaseResponseBody onLoanConfirmed(MobyEventBody eventBody) throws Exception {
        if (null == eventBody) throw new SapBizException("无效事件, 消息内容=null");

        setupConverterContext(eventBody);
        orderService.onLoanConfirmed(eventBody);

        return BaseResponseBody.allOk();
    }

    @Override
    public BaseResponseBody onInsuranceConfirmed(MobyEventBody eventBody) throws Exception {
        if (null == eventBody) throw new SapBizException("无效事件, 消息内容=null");

        setupConverterContext(eventBody);
        orderService.onInsuranceConfirmed(eventBody);

        return BaseResponseBody.allOk();
    }

    @Override
    public BaseResponseBody onOfferBoutiqueOrMaintain(MobyEventBody eventBody) throws Exception {
        if (null == eventBody) throw new SapBizException("无效事件, 消息内容=null");

        setupConverterContext(eventBody);
        orderService.onOfferBoutiqueOrMaintain(eventBody);

        return BaseResponseBody.allOk();
    }

    @Override
    public BaseResponseBody onConsultChanged(MobyEventBody eventBody) throws Exception {
        if (null == eventBody) throw new SapBizException("无效事件, 消息内容=null");

        setupConverterContext(eventBody);
        orderService.onConsultChanged(eventBody);

        return BaseResponseBody.allOk();
    }

    @Override
    public BaseResponseBody onBuyerIndex(MobyEventBody eventBody) throws Exception {
        if (null == eventBody) throw new SapBizException("无效事件, 消息内容=null");
        bpService.createOrExtendBPIfNesessary(eventBody);
        return BaseResponseBody.allOk();
    }

    @Override
    public BaseResponseBody onBoutiqueChanged(MobyEventBody eventBody) throws Exception {
        if (null == eventBody) throw new SapBizException("无效事件, 消息内容=null");

        setupConverterContext(eventBody);
        orderService.onBoutiqueChanged(eventBody);

        return BaseResponseBody.allOk();
    }

    @Override
    public BaseResponseBody onDownPaymentConfirmed(MobyEventBody eventBody) throws Exception {
        if (null == eventBody) throw new SapBizException("无效事件, 消息内容=null");

        setupConverterContext(eventBody);
        fundService.onDownPaymentConfirmed(eventBody);

        return BaseResponseBody.allOk();
    }

    @Override
    public BaseResponseBody onBilling(MobyEventBody eventBody) throws Exception {
        if (null == eventBody) throw new SapBizException("无效事件, 消息内容=null");

        setupConverterContext(eventBody);
        orderService.onBilling(eventBody);
        return BaseResponseBody.allOk();
    }

    @Override
    public BaseResponseBody onFinalPaymentConfirmed(MobyEventBody eventBody) throws Exception {
        if (null == eventBody) throw new SapBizException("无效事件, 消息内容=null");

        setupConverterContext(eventBody);
        fundService.onFinalPaymentConfirmed(eventBody);
        return BaseResponseBody.allOk();
    }


    @Override
    public BaseResponseBody onVehicleStockOut(MobyEventBody eventBody) throws Exception {
        if (null == eventBody) throw new SapBizException("无效事件, 消息内容=null");

        setupConverterContext(eventBody);
        orderService.onVehicleStockOut(eventBody);
        return BaseResponseBody.allOk();
    }

    @Override
    public BaseResponseBody onBoutiqueStockOut(MobyEventBody eventBody) throws Exception {
        if (null == eventBody) throw new SapBizException("无效事件, 消息内容=null");

        setupConverterContext(eventBody);
        orderService.onBoutiqueStockOut(eventBody);
        return BaseResponseBody.allOk();
    }
}
